﻿$(document).ready(function() {
	$(".tab-main").each(function(i){
		var height = window.screen.availHeight;
		$(this).css("height",(height-250)+"px");
	});
	
	$(".tab").on("click",function(){
		$(".tab").each(function(){
			$(this).css("background-color","#f1f1f1");
			$(this).css("border","1px solid #dddddd");
			
			var innerSrc = $(this).find("img").attr("src");
			var newSrc = innerSrc.replace(/_pr/, "");
			$(this).find("img").attr("src",newSrc);
		});
		
		$(this).css("border","0px");
		$(this).css("border-top","3px solid #03a9f4");
		$(this).css("background-color","white");
		
		var src = $(this).find("img").attr("src");
		$(this).find("img").attr("src",src.replace(/.png/, "_pr.png"));
		
		var index = $(this).index();
		$(".tab-main").each(function(i){
			if(i==index){
				$(this).css("display","block");
				$(this).addClass("animated fadeInRight");
			}else{
				$(this).css("display","none");
				$(this).removeClass("animated fadeInRight");
			}
		});
	});
	
	$.fn.extend({
		drag: function (titleDiv) {
		    var M = false;
		    var Rx, Ry;
		    var t = $(this);
			t.mousedown(function (event) {
					Rx = event.pageX - (parseInt(t.css("left")) || 0);
					Ry = event.pageY - (parseInt(t.css("top")) || 0);
					
					var titleWidth = $("#"+titleDiv).width();
					var titleHeight = $("#"+titleDiv).height();
					
					if(Rx>titleWidth || Ry>titleHeight){
						//alert(1);
						M = false;
					}else{
						t.fadeTo(20, 0.5);
						M = true;
					}
					
			})
			.mouseup(function (event) {
				M = false; 
				t.fadeTo(20, 1);
			});
		    $(document).mousemove(function (event) {
		        if (M) {
		            t.css({ top: event.pageY - Ry, left: event.pageX - Rx });
		        }
		    });
		},
		initComboTree:function(data){
			var me = $(this);
			var ztreeId = $(this).attr("id")+"Tree";
			if( $.fn.zTree.getZTreeObj(ztreeId)){
				$("#"+ztreeId).show();
				$("#"+ztreeId).width($(this).width()+13);
				return ;
			}
			
			$(this).after($("<ul id='"+ztreeId+"' class='ztree combotree' ></ul>"));
			
			var zTreeObj;
			var setting = {
				callback:{
					onClick:function(event, treeId, treeNode){
						me.val(treeNode.name);
						me.attr("_value",treeNode.id);
						$("#"+ztreeId).hide();
					}
				},
				view:{
					showLine:false
				}
			};
			
			zTreeObj = $.fn.zTree.init($("#"+ztreeId), setting, data);
			
			$(document).on("click", function(e){  
				$("#"+ztreeId).hide();
			});
			
			$("#"+ztreeId).click(function(event){  
			    event.stopPropagation();  
			});  
			
			$(this).click(function(event){
				$(this).initComboTree(data);
				event.stopPropagation();  
			});
		},
		getComboTreeValue:function(){
			 if($(this).attr("_value")){
				 return $(this).attr("_value");
			 }else{
				 return null;
			 }
		},
		setComboTreeValue:function(value){
			var tree = $.fn.zTree.getZTreeObj($(this).attr("id")+"Tree");
			var node = tree.getNodeByParam("id",value);
			tree.selectNode(node);
			$(this).val(node.name);
			$(this).attr("_value",node.id);
		}
	});
});

(function () {
    $.Model = {
        show: function () {
        	 var oDiv = document.createElement('div');
	   		 oDiv.id="modelDiv";
	   		 oDiv.style = "position:fixed;top: 0px;right:0px;bottom:0px;filter: alpha(opacity=50);"+
	   		 "background-color: black;z-index: 1002;left: 0px;"+
	   		 "opacity:0.5;-moz-opacity:0.5;width:100%";
	   		 document.body.appendChild(oDiv);
	   		 $("#modelDiv").height(document.body.clientHeight);
        },
        hide:function(){
        	$("#modelDiv").remove();
        }
    }
})();

(function () {
    $.MsgBox = {
        Alert: function (title, msg,callback) {
            GenerateHtml("alert", title, msg);
            btnOk(callback);  
        },
        Confirm: function (title, msg, callback) {
            GenerateHtml("confirm", title, msg);
            btnOk(callback);
            btnNo();
        }
    }
    
    var btnOk = function (callback) {
        $(".a-btn-ok").click(function () {
        	  $(this).parent().parent().remove();
	   		  //移除遮罩层
        	  $.Model.hide();
	          if (typeof (callback) == 'function') {
	              callback();
	          }
        });
      };
      
      var btnNo = function () {
    	    $(".a-btn-off").click(function () {
	        	  $(this).parent().parent().remove();
		   		  //移除遮罩层
	        	  $.Model.hide();
	        });
	   };
      
      var  GenerateHtml = function(type,title,msg,level){
    		 //显示遮罩层
    	  	 $.Model.show();
    		  
    		 var oDiv = document.createElement('div');
    		 oDiv.id="myDiv";
    		 var html = "<div id='myTitle' style='cursor:pointer;width:100%;height:40px;background-color:#1994e2;text-align:center;line-height:40px;color:white'>"+title+"</div>";
    		 html += "<div style='width:100%;height:110px;background-color:#ffffff;color:#444444;font-size;18px;word-break:break-all; text-align:center;'><div  id='msgDiv' >"+msg+"</div></div>";
    		 if(type=="alert"){
    			 html += "<div style='width:100%;height:40px;text-align:center;'><a class='a-btn-ok'>好的</a></div>";
    		 }else{
    			 html += "<div style='width:100%;height:40px;text-align:center;'><a class='a-btn-ok'>确认</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class='a-btn-off'>取消</a></div>";
    		 }
    		 oDiv.innerHTML = html;
    		 oDiv.style = "background-color:#ffffff;position:absolute;border:1px solid #1994e2;z-index:99999";
    		 document.body.appendChild(oDiv);
    		 
    		 var iWidth=350; //弹出窗口的宽度;
    		 var iHeight=190; //弹出窗口的高度;
    		 var iTop = (document.documentElement.clientHeight-130-iHeight)/2-50; //获得窗口的垂直位置;
    		 var iLeft = (document.documentElement.clientWidth-iWidth)/2-50; //获得窗口的水平位置;
    		 $("#myDiv").height(iHeight)
    	       .width(iWidth)
    	       .css("top",iTop)
    	       .css("left",iLeft);
    		  var height = $("#msgDiv").height();
    		  if(height>110){
    			  $("#msgDiv").parent().css("overflow-y","scroll");
    			  $("#msgDiv").parent().css("overflow-x","hidden");
    		  }else{
    			  var paddingTop = (110-height)/2;
    			  $("#msgDiv").css("padding-top",paddingTop);
    		  }
    		  
    		  $("#myDiv").drag("myTitle");
    	}
})();


